//
// MainWindow.cs
// 
// Application's main window.
//
// Author:
//   Radu Tofoleanu <kenjiru.ro@gmail.com>
//
// Copyright (C) 2007-2008 Radu Tofoleanu (http://kenjiru.ro)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using System.Data;
using System.Data.OracleClient;
using System.Xml.Serialization;
using Gtk;
using EigaLib;

namespace EigaApp {
	public partial class MainWindow: Gtk.Window
	{
		// lucrul cu baza de date
		private EigaDb eigaDb = new EigaDb ();
		// taburile pt. Notebook
		private MoviesTab moviesTab = null;
		private PeopleTab peopleTab = null;
		private OthersTab othersTab = null;

		public MainWindow (): base ("")
		{
			Build ();
			
			this.Maximize ();
			
			moviesTab = new MoviesTab();
			peopleTab = new PeopleTab();
			othersTab = new OthersTab();
			mainNotebook.AppendPage(moviesTab, new Label("Movies"));
			mainNotebook.AppendPage(peopleTab, new Label("People"));
			mainNotebook.AppendPage(othersTab, new Label("Others"));
			
			// ca sa fim siguri
			Connect.Label = "Connect";
			
			Refresh ();
		}
		
		private void ErrorMessage(string msg)
		{
			MessageDialog msgDlg = new MessageDialog (this, 
				DialogFlags.DestroyWithParent,
				MessageType.Error,
				ButtonsType.Close, 
				msg );
			msgDlg.Run ();
			msgDlg.Destroy ();
		}
		
		protected void OnDeleteEvent(object sender, DeleteEventArgs a)
		{
			Application.Quit ();
			a.RetVal = true;
		}
		
		protected virtual void OnExitActivated(object sender, System.EventArgs e)
		{
			Application.Quit ();
		}

		protected virtual void OnAboutActivated(object sender, System.EventArgs e)
		{
			Gtk.AboutDialog aboutDlg = new Gtk.AboutDialog ();
			
			string[] arrDocumenters = new string[1];
			arrDocumenters[0] = "Tofoleanu Radu";
			
			aboutDlg.Name = "EigaApp";
			aboutDlg.License = "Realised under the GNU General Public Licence";
			aboutDlg.Version = "0.2.1";
			aboutDlg.Website = "http://www.kenjiru.ro";
			aboutDlg.Comments = "Acest program reprezinta proiectul final pentru disciplina IP";
			aboutDlg.Documenters = arrDocumenters; 
			
			aboutDlg.Show ();
		}

		protected virtual void OnSettingsActivated(object sender, System.EventArgs e)
		{
			SettingsDialog settingsDlg = new SettingsDialog();
			settingsDlg.Show ();
		}

		protected virtual void OnMainNotebookSwitchPage(object o, Gtk.SwitchPageArgs args)
		{
			Refresh();
		}
		
		protected virtual void OnConnectActivated (object sender, System.EventArgs e)
		{
			if (Connect.Label == "Connect") {
				if (eigaDb.Connect ()) {
					if (eigaDb.Login ()) {
						Connect.StockId = "gtk-disconnect";	
						Connect.Label = "Disconnect";
						Connect.ShortLabel = "Disconnect";
					} else {
						ErrorMessage ("M-am conectat la baza de date, dar nu m-am putut autentifica!");
						Console.WriteLine ("MainWIndow.cs: OnConnectActivated(): Nu m-am putut autentifica!");
					}
				} else {
					ErrorMessage ("Nu m-am putut conecta la baza de date!");
					Console.WriteLine ("MainWIndow.cs: OnConnectActivated(): Nu m-am putut conecta la baza de date!");
				}
			} else {
				Connect.StockId = "gtk-connect";
				Connect.Label = "Connect";
				Connect.ShortLabel = "Connect";
				
				eigaDb.Disconnect();
			}
			Refresh();
		}
		
		private void Refresh()
		{
			if (eigaDb == null)
				return;
			
			switch (mainNotebook.CurrentPage) {
				case 0:
					moviesTab.DbConn = eigaDb;
					moviesTab.Refresh();
					break;
				case 1:
					peopleTab.DbConn = eigaDb;
					peopleTab.Refresh();
					break;
				case 2:
					othersTab.DbConn = eigaDb;
					othersTab.Refresh();
					break;
			}
		}
	}
}